package com.hzw.saas.service.msg.tool;

import java.util.*;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.hzw.saas.api.group.IGroupService;
import com.hzw.saas.api.group.bo.GroupBo;
import com.hzw.saas.api.msg.bo.MsgBo;
import com.hzw.saas.api.user.IUserProfileService;
import com.hzw.saas.api.user.bo.UserProfileBO;
import cn.hutool.core.util.ReUtil;
import org.springframework.stereotype.Component;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 消息内容解析器
 * </p>
 *
 * @author sonam
 * @since 2021/3/8 16:52
 */
@Component
@RequiredArgsConstructor
public class MsgContentParser {

    private static final ThreadLocal<Map<String, String>> local = new ThreadLocal<>();
    private final IUserProfileService userProfileService;
    private final IGroupService groupService;

    public void parser(List<MsgBo> msgBoList) {
        local.set(new HashMap<>());
        msgBoList.forEach(msgBo -> {
            String msg = msgBo.getMsg();
            List<String> realNameElList = ReUtil.findAll("realName\\{(.*?)\\}", msg, 0, new ArrayList<>());
            msg = dealMsgText(msg, realNameElList);
            List<String> groupNameText = ReUtil.findAll("groupName\\{(.*?)\\}", msg, 0, new ArrayList<>());
            msg = dealMsgText(msg, groupNameText);

            msgBo.setMsg(msg);
        });
        local.remove();
    }

    /**
     * 将消息内容的ID转化为真实的文本
     *
     * @param msg
     * @param elList
     * @return
     */
    private String dealMsgText(String msg, List<String> elList) {
        if (CollectionUtils.isEmpty(elList)) {
            return msg;
        }

        // 相当于一个缓存
        Map<String, String> map = local.get();
        for (String el : elList) {
            String id = el.substring(el.indexOf("{") + 1, el.lastIndexOf("}"));
            String name = map.get("id");
            if (Objects.nonNull(name)) {
                return msg.replaceAll(el, name);
            }

            if (el.startsWith("realName")) {
                name = getUserRealName(id);

            } else if (el.startsWith("groupName")) {
                name = getGroupName(id);
            }
            map.put(id, name);
            msg = msg.replace(el, name);
        }
        return msg;
    }

    private String getGroupName(String id) {
        ValueWrapper valueWrapper = parsing(id);
        GroupBo groupBo = groupService.getGroupById(valueWrapper.id);
        if (Objects.isNull(groupBo) || StringUtil.isBlank(groupBo.getGroupName())) {
            return valueWrapper.defValue;
        } else {
            return groupBo.getGroupName();
        }
    }

    private String getUserRealName(String id) {
        ValueWrapper valueWrapper = parsing(id);
        UserProfileBO userProfileBO = userProfileService.get(valueWrapper.id);
        if (Objects.isNull(userProfileBO) || StringUtil.isBlank(userProfileBO.getRealName())) {
            return valueWrapper.defValue;
        } else {
            return userProfileBO.getRealName();
        }
    }

    private ValueWrapper parsing(String id) {
        String valueId;
        String defValue;
        String[] split = id.split("##");
        try {
            valueId = split[0];
            defValue = split[1];
        } catch (Exception e) {
            defValue = "null";
            valueId = id;
        }

        return new ValueWrapper(valueId, defValue);
    }

    class ValueWrapper {
        String id;
        String defValue;

        public ValueWrapper(String id, String defValue) {
            this.id = id;
            this.defValue = defValue;
        }
    }


}
